package function;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import UserDatabase.HouseDatabase;


public class MHouseFrame extends JFrame{
	
	String [] tableHead;
	String [][] house;
	JTable table;
	JLabel l_uno,l_hno,l_state;
	JTextField t_uno,t_hno,t_state;
	JButton b_find,b_reset,b_refresh,b_updata,b_insert;
	HouseDatabase h = new HouseDatabase();
	String SQL;
	
	public MHouseFrame(String SQL) {
		this.setSize(800,500);
		this.setTitle("房间管理");
		this.setLocation(500,150);
		this.SQL = SQL;
		init();
		this.setVisible(true);
	}
	
	public void init() {
		h.setSQL(SQL);
		house = h.getRecord();
		tableHead = h.getColumnName();
		table = new JTable(house,tableHead);
		
		l_uno = new JLabel("单元号",JLabel.CENTER);
		l_hno = new JLabel("房间号",JLabel.CENTER);
		l_state = new JLabel("状态",JLabel.CENTER);
		
		t_uno = new JTextField();
		t_hno = new JTextField();
		t_state = new JTextField();
		
		b_find = new JButton("查询");
		b_find.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String SQL = "select * from house where";
				String uno = t_uno.getText();
				String hno = t_hno.getText();
				String state = t_state.getText();
				if(uno.equals("")&&hno.equals("")&&state.equals("")) {
					new MHouseFrame("select * from house");
					setVisible(false);
				}else if(!uno.equals("")) {
					
					SQL = SQL+" uno = '"+uno+"' ";
					new MHouseFrame(SQL);
					setVisible(false);
				
				}else if(!hno.equals("")) {
				
					SQL = SQL+" hno = '"+hno+"' ";
					new MHouseFrame(SQL);
					setVisible(false);
					
				}else if(!state.equals("")) {
				
					SQL = SQL+" state = '"+state+"' ";
					new MHouseFrame(SQL);
					setVisible(false);
					
				}
			}
			
		});
		
		b_reset = new JButton("重置");
		b_reset.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				t_uno.setText(null);
				t_hno.setText(null);
				t_state.setText(null);
			}
			
		});
		
		b_updata = new JButton("更新房间状态");
		b_updata.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new UpdateHouseFrame();
			}
			
		});
		
		b_refresh = new JButton("刷新");
		b_refresh.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new MHouseFrame("select * from house");
				setVisible(false);
			}
			
		});
		
		b_insert = new JButton("新增房间");
		b_insert.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new InsertHouseFrame();
			}
			
		});
		
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1,11,5,5));
		p.add(l_uno);
		p.add(t_uno);
		p.add(l_hno);
		p.add(t_hno);
		p.add(l_state);
		p.add(t_state);
		p.add(b_find);
		p.add(b_reset);
		p.add(b_refresh);
		
		JScrollPane sp = new JScrollPane(table);
		
		JPanel p_bottom = new JPanel();
		p_bottom.setLayout(new GridLayout(1,2,8,8));
		p_bottom.add(b_updata);
		p_bottom.add(b_insert);
		
		this.add(p,BorderLayout.NORTH);
		this.add(sp,BorderLayout.CENTER);
		this.add(p_bottom,BorderLayout.SOUTH);
	}
	
	
}
